package com.tuanzi.blog.web;

import com.tuanzi.blog.model.Comment;
import com.tuanzi.blog.service.BlogService;
import com.tuanzi.blog.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 雄关漫道真如铁，而今迈过从头越
 **/

@Controller
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private BlogService blogService;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model){

        model.addAttribute("comments",commentService.listCommentByBlogId(blogId));
        System.out.println("我跳转了==============================================================");

        return "blog :: commentList";
    }

    @PostMapping("/comments")
    public String post(Comment comment, RedirectAttributes redirectAttributes){
        commentService.saveComment(comment);
        return "redirect:/comments/"+comment.getBlog().getId();
    }

}
